<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<link rel="stylesheet" type="text/css" href="../../css/weui.min.css" />
		<link rel="stylesheet" type="text/css" href="../../css/jquery-weui.min.css" />
		<link rel="stylesheet" type="text/css" href="../../css/index.css" />
		<link rel="stylesheet" type="text/css" href="../../css/order.css" />
		<link rel="stylesheet" type="text/css" href="https://at.alicdn.com/t/font_546019_o8qxm6fupb0ggb9.css" />
		<title>添加物料</title>
		<script src="../../js/jquery-2.1.4.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/jquery-weui.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/baseConfig.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<style type="text/css">
		.thooseMaterial {
			position: fixed;
			top: 20%;
			left: 3.375rem;
			width: 12rem;
			height: 14rem;
			border: 1px solid #e2e2e2;
			background: #fff;
			border-radius: 0.5rem;
		}
		
		.thooseMaterial p {
			height: 2rem;
			line-height: 2rem;
			background: #28C81E;
			text-align: center;
			color: #fff;
			position: relative;
		}
		
		.thooseMaterial  span {
			    position: absolute;
			    right: 0.5rem;
			    top: 0.4rem;
			    color: #fff;
		}
		
		.thooseMaterial ul {
			height: 12rem;
			overflow-y: scroll;
		}
		
		.thooseMaterial ul li {
			width: 8rem;
			height: 1.5rem;
			line-height: 1.5rem;
			text-align: center;
			color: #000;
			font-size: 0.65rem;
			margin: 0.5rem auto;
			border: 1px solid #e2e2e2;
		}
		
		.handBtn {
			display: none;
			width: 100%;
			height: auto;
			overflow: hidden;
		}
		
		.handBtn .handBtnList p {
			width: 100%;
			padding: 0 5%;
			height: 2.5rem;
			line-height: 2.5rem;
			font-size: 0.65rem;
		}
		
		.handBtn .handBtnList p span {
			width: 11rem;
		}
		
		.handBtn .handBtnList p input {
			border: 1px solid #e2e2e2 !important;
			background-color:#fff;
			width: 3rem;
			font-size: 0.65rem !important;
			padding: 0.1rem 0.3rem;
			box-sizing: border-box;
		}
	</style>

	<body>
		<!--<div id="header">
			<div class="header-title" style="position: fixed;top: 0;">
				<img src="../../img/left.png" class="left" />
				<span>添加物料</span>
			</div>
		</div>-->
		<div class="editMaterial" style="margin-top: 0;">
			<p>
				<span>物料名称</span>
				<i class="BtnMaterName"><input type="" name="" id="" value="" class="materialName" placeholder="请选择物料" disabled="disabled"/></i>
			</p>
			<p>
				<span>混合物料</span>
				<input type="" name="" id="" value="" class="isHunhe" disabled="disabled" />
			</p>
			<div class="handBtn">
			</div>
			<p>
				<span>物料重量</span>
				<input type="tel" name="" id="" value="" class="materialNum" placeholder="请填写物料重量" style="width: 9rem;" />
				<em>吨</em>
			</p>
			<p>
				<span>物料单价</span>
				<input type="tel" name="" id="" value="" placeholder="请填写物料单价" class="materialUnit" />
				<em>元/吨</em>
			</p>
			<p>
				<span>含杂不超</span>
				<input type="tel" name="" maxlength="2"  id="" value="" placeholder="含杂比例不超过" class="proportion" />
				<em>%</em>
			</p>
			<!--<p>
				<span>利润率</span>
				<input type="" name="" id="" value="" class="rate"/>
				<em>%</em>
			</p>-->
			<div class="demos-content-padded" style="margin-top: 1.5rem;">
				<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default" style="float: left;background: #e5e5e5;">取消</a>
				<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_primary" style="float: right;margin-top: 0;background: #28c81e;">确定</a>
			</div>
		</div>
		<div class="thooseMaterial" style="display: none;">
			<p>选择物料</p>
			<span>X</span>
			<ul class="ul_warp">
				<li></li>
			</ul>
		</div>
	</body>
	<script type="text/javascript">
		$(document).ready(function() {
			baseConfig.historyBack();
			var type = baseConfig.GetQueryString("type")
			var orderNo = baseConfig.GetQueryString("orderNo")
			var orderid = baseConfig.GetQueryString("orderid")
			picker(orderNo)
			var temp = ""; //混合物料的物料编号集合
			var temp2 = ""; //混合物料的组成比例集合
			function str(str){
				var str = str;
				var arr = str.split("-");
				var arr1 = arr[arr.length-1];
				var arr2 = arr[arr.length-2];
				var String = arr2 + "-" +arr1
				return String
			}
			function picker(orderNo) {
				$.ajax({
					type: "get",
					url: baseConfig.localHttp + "api/material/getTopMaterialList",
					async: false,
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data: {
						orderNo: orderNo,
						clientId: baseConfig.login()
					},
					success: function(data) {
						console.log(data)
						var Data = data.content;
						if(data.code == 100) {
							var $li = "";
							for(var i = 0; i < Data.length; i++) {
								$li += "<li data-material=" + Data[i].materialNo + " data-hunhe=" + Data[i].isHunheLabel + ">" + Data[i].materialName + "</li>"
							}
							$(".ul_warp").html($li)
							for(var j = 0; j < $(".ul_warp li").length; j++) {
								$(".ul_warp li").eq(j).on("click", function() {
									sessionStorage.setItem("goodsNo", $(this).attr("data-material"))
									$(".materialName").val($(this).html())
									if($(this).attr("data-hunhe")=="null"){
										$(".isHunhe").val("")
									}else{
										$(".isHunhe").val($(this).attr("data-hunhe"))
									}
									
									clickNext($(this).attr("data-material"))
								})
							}

							function clickNext(materialNo) {
								$.ajax({
									type: "get",
									url: baseConfig.localHttp + "api/material/getNextMaterialList",
									async: false,
									headers: {
										'Content-Type': 'application/x-www-form-urlencoded'
									},
									data: {
										materialNo: materialNo
									},
									success: function(data) {
										if(data.code == 100) {
											var $li = "";
											var Data = data.content;
											for(var i = 0; i < Data.length; i++) {
												$li += "<li data-material=" + Data[i].materialNo + " data-hunhe=" + Data[i].isHunheLabel + ">" + Data[i].materialName + "</li>"
											}
											$(".ul_warp").html($li)
											for(var j = 0; j < $(".ul_warp li").length; j++) {
												$(".ul_warp li").eq(j).on("click", function() {
													sessionStorage.setItem("goodsNo", $(this).attr("data-material"))
													$(".materialName").val(str($(".materialName").val()+"-"+$(this).html()))
													if($(this).attr("data-hunhe")=="null"){
														$(".isHunhe").val("")
													}else{
														$(".isHunhe").val($(this).attr("data-hunhe"))
													}
													clickNext($(this).attr("data-material"))
												})
											}
										} else {
											$(".thooseMaterial").hide();
											if($(".isHunhe").val() == "是") {
												$(".handBtn").show()
												addHunheLabel(materialNo)
											} else {
												$(".handBtn").hide()
											}
											return
										}
									},
									error: function() {
										$.hideLoading();
										$.toast("请求失败", "cancel");
									}
								})
							}
						} else {
							$.toptip(data.content, 1200, 'error');
						}
					},
					error: function() {
						$.hideLoading();
						$.toast("请求失败", "cancel");
					}
				})
			}
			$(".BtnMaterName").on("click", function() {
				picker(orderNo)
				$(".thooseMaterial").show()
			})
			$(".thooseMaterial span").on("click",function(){
				$(".thooseMaterial").hide()
				$(".materialName").val("")
				$(".isHunhe").val("")
			})
			function addHunheLabel(materialNo) {
				$.ajax({
					type: "get",
					url: baseConfig.localHttp + "api/material/getGroupMaterialList",
					async: false,
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data: {
						materialNo: materialNo
					},
					success: function(data) {
						console.log(data)
						var $div = "";
						for(var i = 0; i < data.content.length; i++) {
							temp += data.content[i].materialNo+","
							$div += "<div class='handBtnList'>"
							$div += "<p><span>"+data.content[i].materialName+"</span><input type='tel' value='' class='Num' maxlength='2' />%</p></div>"
						}
						$(".handBtn").empty()
						$(".handBtn").append($div)
					}
				})
			}

			$(".weui-btn_primary").on("click", function() {
				var mater = sessionStorage.getItem("goodsNo")
				var counts = $(".materialNum").val();
				var unitPrice = $('.materialUnit').val();
				var zizhiPercent = $(".proportion").val();
				if(!mater || !counts || !unitPrice || !zizhiPercent) {
					$.toptip("信息填写不全", 1200, 'error');
				} else {
					if(orderNo == "null") {
						if($(".isHunhe").val() == "是") {
							for(var j = 0; j < $(".handBtnList p input").length; j++) {
								temp2 += $(".handBtnList p .Num").eq(j).val()+","
							}
							send(temp, temp2,mater,counts,unitPrice,zizhiPercent,"")
						} else {
							send("", "",mater,counts,unitPrice,zizhiPercent,"")
						}
					} else {
						if($(".isHunhe").val() == "是"){				
							for(var j = 0; j < $(".handBtnList p .Num").length; j++) {
								temp2 += $(".handBtnList p .Num").eq(j).val()+","
							}
							$.ajax({
								type: "get",
								url: baseConfig.localHttp2 + "api/order/addOrEditMaterial",
								async: false,
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded',
								},
								data: {
									clientId:baseConfig.login(),
									orderNo: orderNo,
									materialNo: mater,
									counts: counts,
									fixNo:temp,
									fixPercent:temp2,
									unitPrice: unitPrice,
									zizhiPercent: zizhiPercent,
								},
								success: function(data) {
									if(data.code == 100) {
										sessionStorage.removeItem("mater")
										location.href = "materialList.html?orderNo=" + orderNo + "&orderid=" + orderid
									} else {
										$.toptip(data.content, 1200, 'error');
									}
								},
								error: function() {
	
								}
							})
						} else {
							$.ajax({
								type: "get",
								url: baseConfig.localHttp2 + "api/order/addOrEditMaterial",
								async: false,
								headers: {
									'Content-Type': 'application/x-www-form-urlencoded',
								},
								data: {
									clientId:baseConfig.login(),
									orderNo: orderNo,
									materialNo: mater,
									counts: counts,
									unitPrice: unitPrice,
									fixNo:"",
									fixPercent:"",
									zizhiPercent: zizhiPercent,
								},
								success: function(data) {
									if(data.code == 100) {
										sessionStorage.removeItem("mater")
										location.href = "materialList.html?orderNo=" + orderNo + "&orderid=" + orderid
									} else {
										$.toptip(data.content, 1200, 'error');
									}
								},
								error: function() {
	
								}
						})
						}
						
					}
				}
			})
			//封装函数
			function send(temp,temp2,mater,counts,unitPrice,zizhiPercent,orderNo2) {
						$.ajax({
							type: "get",
							url: baseConfig.localHttp + "api/order/addOrEditMaterial",
							async: false,
							headers: {
								'Content-Type': 'application/x-www-form-urlencoded',
							},
							data: {
								clientId:baseConfig.login(),
								orderNo: orderNo2,
								materialNo: mater,
								counts: counts,
								unitPrice: unitPrice,
								fixNo: temp,
								fixPercent: temp2,
								zizhiPercent: zizhiPercent,
							},
							success: function(data) {
								if(data.code == 100) {
									sessionStorage.removeItem("mater")
									var content = data.content
									location.href = "materialList.html?orderNo=" + content + "&orderid=" + content
								} else {
									$.toptip(data.content, 1200, 'error');
								}
							},
							error: function() {

							}
						})
					
			}
			//点击取消
			$(".weui-btn_default").on("click", function() {
				location.href = "orderList.html"
				//				location.href = "http://www.hsw.com/a/weixinMyCenter"
			})
		})
	</script>

</html>